'''
@author: pnuzhnyi

#http://www.facebook.com/connect/uiserver.php?client_id=147724165255239&redirect_uri=http%3A%2F%2Fapps.facebook.com%2Ffantasy_surf%2F&display=page&next=https%3A%2F%2Fgraph.facebook.com%2Foauth%2Fauthorize_success%3Fclient_id%3D147724165255239%26%20%0Aredirect_uri%3Dhttp%3A%2F%2Fapps.facebook.com%2Ffantasy_surf%2F%26type%3Dweb_server&cancel_url=https://graph.facebook.com/oauth/authorize_cancel?client_id=147724165255239&redirect_uri=http%3A%2F%2Fapps.facebook.com%2Ffantasy_surf%2F%26type%3Dweb_server&return_session=1&canvas=1&app_id=147724165255239&method=permissions.request
#https://www.facebook.com/connect/uiserver.php?client_id=111651893463&redirect_uri=http%3A%2F%2Fapps.facebook.com%2Fbiscottodellafortuna%2F&display=page&next=https%3A%2F%2Fgraph.facebook.com%2Foauth%2Fauthorize_success%3Fclient_id%3D111651893463%26redirect_uri%3Dhttp%253A%252F%252Fapps.facebook.com%252Fbiscottodellafortuna%252F%26type%3Dweb_server&cancel_url=https%3A%2F%2Fgraph.facebook.com%2Foauth%2Fauthorize_cancel%3Fclient_id%3D111651893463%26redirect_uri%3Dhttp%253A%252F%252Fapps.facebook.com%252Fbiscottodellafortuna%252F%26type%3Dweb_server&return_session=1&canvas=1&app_id=111651893463&method=permissions.request
#http://www.facebook.com/connect/uiserver.php?postmessage%26frame%3Df37df5c86%26result%3DxxRESULTTOKENxx&display=popup&channel=http://fantasy-surf.appspot.com/fb/%3Ffb_sig_in_iframe%3D1%26fb_sig_iframe_key%3D1f0e3dad99908345f7439f8ffabdffc4%26fb_sig_locale%3Den_US%26fb_sig_in_new_facebook%3D1%26fb_sig_time%3D1282249623.4801%26fb_sig_added%3D0%26fb_sig_country%3Dfr%26fb_sig_api_key%3Dcfefd01fe16410b2608796c1ee021d9d%26fb_sig_app_id%3D147724165255239%26fb_sig%3D34fa4b1901cd774a75a78d15fe89db4f&cancel_url=http://static.ak.fbcdn.net/connect/xd_proxy.php%23cb%3Df13c354d9%26origin%3Dhttp%253A%252F%252Ffantasy-surf.appspot.com%252Ff3acd4f10%26relation%3Dopener%26transport%3Dpostmessage%26frame%3Df37df5c86%26result%3DxxRESULTTOKENxx&locale=en_US&return_session=1&session_version=3&fbconnect=0&canvas=0&legacy_return=1&method=permissions.request
#http://www.facebook.com/connect/uiserver.php?client_id=147724165255239&redirect_uri=http%3A%2F%2Fapps.facebook.com%2Fbiscottodellafortuna%2F&display=page&next=https%3A%2F%2Fgraph.facebook.com%2Foauth%2Fauthorize_success%3Fclient_id%3D111651893463%26redirect_uri%3Dhttp%253A%252F%252Fapps.facebook.com%252Fbiscottodellafortuna%252F%26type%3Dweb_server&cancel_url=https%3A%2F%2Fgraph.facebook.com%2Foauth%2Fauthorize_cancel%3Fclient_id%3D111651893463%26redirect_uri%3Dhttp%253A%252F%252Fapps.facebook.com%252Fbiscottodellafortuna%252F%26type%3Dweb_server&return_session=1&canvas=1&app_id=111651893463&method=permissions.request
'''

from django.http import HttpResponse
from django.shortcuts import render_to_response
from lib.authentification import is_authenticated, get_user_id
from settings import FACEBOOK_APP_ID
from surfers.repository import surfer_repository
from gear.repository import gear_repository
from events.repository import events_repository
from shop.repository import shop_repository
from events.workers import participate as participate_in_event
from events.workers import generate_event_result_notification 
from shop.workers import buy_item


def events(request):
    if is_authenticated(request):
        upcoming_events = events_repository().get_upcoming_events()
        return render_to_response("events.html",{"events":upcoming_events,"user_id":get_user_id(request)})
    else: return render_to_response("install.html",{"facebook_app_id":FACEBOOK_APP_ID})

def event_participate(request,event_id):
    if is_authenticated(request):
        user_id = get_user_id(request)
        surfer = surfer_repository().get_or_create_surfer(user_id)
        event = events_repository().get_event_details(event_id)
        if participate_in_event(event, surfer):
            return render_to_response("event.participation.confirmation.html",{"event":event})
        else: return HttpResponse("you cannot participate in this event. make sure you have enough money to participate.")
    else: return render_to_response("install.html",{"facebook_app_id":FACEBOOK_APP_ID})

def shop(request):
    if is_authenticated(request):
        items = shop_repository().get_items_for_sale()
        return render_to_response("shop.html",{"shop_inventory":items})
    else: return render_to_response("install.html",{"facebook_app_id":FACEBOOK_APP_ID})

def shop_buy_item(request,item_id):
    if is_authenticated(request):
        user_id = get_user_id(request)
        surfer = surfer_repository().get_or_create_surfer(user_id)
        item = shop_repository().get_item_by_id(item_id)
        if buy_item(item, surfer):
            return render_to_response("shop.item.bought.html",{"item":item})
        else: return HttpResponse("can't buy this item. make sure you have enough money and the item is available")
    else: return render_to_response("install.html",{"facebook_app_id":FACEBOOK_APP_ID})
    
def profile(request):    
    if is_authenticated(request):
        user_id = get_user_id(request)
        
        profile = surfer_repository().get_or_create_surfer(user_id)
        gear = gear_repository().get_gear_for_user(user_id)
        upcoming_events = events_repository().get_upcoming_events_for_surfer(user_id)
        wins = generate_event_result_notification(user_id)
        
        return render_to_response("profile.html",{"profile":profile,"gear":gear,"events":upcoming_events,"wins":wins})
    else: return render_to_response("install.html",{"facebook_app_id":FACEBOOK_APP_ID})
    
def mark_notification_as_read(request, notification_id):
    e_repo = events_repository()
    winner = e_repo.get_winner_by_id(notification_id)
    if winner is not None:
        winner.notified = True
        e_repo.update_event_winner(winner)
    return HttpResponse("ok")